<?php

namespace App\Models;

use App\Constant\ConstantMap;
use App\Services\Common\AdminService;
use Carbon\Carbon;

/**
 * App\Models\Admin
 *
 * @property int $id
 * @property string $user_name 登录名称
 * @property string $real_name 姓名
 * @property string $avatar 头像
 * @property string $mobile 手机号
 * @property string $pass_word 账户密码
 * @property int $role_id 角色ID，管理roles表ID
 * @property int $is_enable 是否启用：1是 0否
 * @property int $is_delete 是否删除：1是 0否
 * @property string $token 登录token
 * @property string $last_login_ip 上次登录IP
 * @property string $last_login_at 上次登录时间
 * @property \Illuminate\Support\Carbon $created_at
 * @property \Illuminate\Support\Carbon $updated_at
 * @property-read \App\Models\AdminRole|null $role
 * @method static \Illuminate\Database\Eloquent\Builder|Admin bgBaseQuery($params = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Admin newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Admin newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel paramsQuery($fields, $params = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Admin query()
 * @method static \Illuminate\Database\Eloquent\Builder|Admin whereAvatar($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Admin whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Admin whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel whereIds($ids = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Admin whereIsDelete($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Admin whereIsEnable($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Admin whereLastLoginAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Admin whereLastLoginIp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Admin whereMobile($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Admin wherePassWord($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Admin whereRealName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Admin whereRoleId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Admin whereToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Admin whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Admin whereUserName($value)
 * @mixin \Eloquent
 */
class Admin extends BaseModel
{
    protected $table = 'admins';

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

        self::creating(function ($model) {
            $model->pass_word = AdminService::syncPassWord(config('common.admin_default_pwd'));
            $model->is_enable = 1;
        });
    }

    public function isLock()
    {
        return !$this->is_enable;
    }

    public function scopeBgBaseQuery($query, $params = [])
    {
        $query->paramsQuery(['user_name', 'real_name', 'mobile', 'role_id', 'is_enable'], $params);
        $query->with(['role']);
    }

    public function setLogin()
    {
        $this->last_login_at = Carbon::now()->toDateTimeString();
        $this->last_login_ip = request()->getClientIp();
        $this->save();
    }

    public static function getForms($fields = [])
    {
        $forms = [
            'user_name'  => ['title' => '用户名', 'type' => 'text', 'desc' => '默认密码为'.config('common.admin_default_pwd')],
            'real_name'  => ['title' => '姓名', 'type' => 'text'],
            'mobile'     => ['title' => '手机号', 'type' => 'text'],
            'is_enable'     => ['title' => '状态', 'type' => 'select', 'options' => map_to_options(ConstantMap::$yesOrNoMap) ],
            'role_id'    => ['title' => '角色', 'type' => 'select', 'options' => AdminRole::select(['id as value', 'name as label'])->get()],
        ];

        $data = [];
        foreach ($fields as $field) {
            if (isset($forms[$field])) {
                $data[$field]          = $forms[$field];
                $data[$field]['field'] = $field;
            }
        }
        return $data;
    }

    public function role()
    {
        return $this->belongsTo(AdminRole::class, 'role_id', 'id');
    }
}
